import {PluginCommand} from "./PluginCommand";
import {
  getAllFastenerWeightInfoApi,
  getFastenerRepositoryListApi,
  getFastenerWeightApi, getPartInfoByPartNumberApi
} from "../util/request/fastenerLibraryApi";
import * as XLSX from "xlsx";
import {request} from "../util/Request";
import {PluginCADToolFunctionType} from "../../nodeApp/static/lib/crowncad-plugin-sdk-0.0.1";

class FastenerStatisticsCommand extends PluginCommand{
  constructor() {
    super();
  }

  getInstanceTree(cb) {
    if(this.query) {
      this.query.getInstanceTree().then(res => {
        cb(res?.data?.instanceTree);
      });
    }
  }
  /**
   * 导入解析
   */
  importProperty(file, cb) {
    getFastenerRepositoryListApi(file).then(res=>{
      cb(res);
    })
  }



  getFastenerWeightByPartNumber(partNumber,cb){
    getFastenerWeightApi({partNumber:partNumber}).then(res => {
      cb(res);
    })
  }

  getAllFastenerWeightInfo(cb){
    getAllFastenerWeightInfoApi().then(res => {
      cb(res);
    })
  }

  handlePickInstance(cb) {
    this.event.subscribeInstancePickEvent(res => (
      cb(res)
    ))
  }

  //通过partNumber获取数据库里的信息
  getPartInfoByPartNumber(params,cb){
    getPartInfoByPartNumberApi(params).then(res => {
      cb(res.data);
    })
  }

  // tips提示接口
  eventBusAlertMsg(message, type) {
    if (this.event) {
      let msg = {type: type, description: message};
      this?.event.messageTip(msg);
    }
  }


  //获取所有的子节点零件,子节点类型，0所有，1零件，2装配
  async getAllChildrenNode(instances,childType = 0){
    return new Promise((resolve) => {
      this.getInstanceTree(res => {

        let allChildList = [];
        for(let resChildIndex = 0;resChildIndex < res.length;resChildIndex++){
          let childList = this.getChild(res[resChildIndex],0);
          for(let childIndex = 0;childIndex < childList.length;childIndex++){
            //结构树上的所有节点
            let curInstance = childList[childIndex];
            //需要的节点
            for(let instanceIndex = 0;instanceIndex < instances.length;instanceIndex++){
              if(instances[instanceIndex].id === curInstance.id){
                //符合节点类型的所有子级节点
                let childTypeList = this.getChild(curInstance,childType);

                allChildList.push(...childTypeList);
              }
            }
          }
        }

        resolve(allChildList);
      })
    })
  }

  //递归获取全部子级节点
  getChild(data,childType) {
    const child = [];

    function traverse(node) {
      if (node.id !== undefined) {
        // TODO 可能存在问题，无法通过二次开发接口获取当前节点是零件还是装配，只能通过是否有子级节点来判断
        if(childType === 0){
          //装配零件
          child.push(node);
        }else if(childType === 1 && (!node.children || node.children.length === 0)){
          //零件
          child.push(node);
        }
        if(childType === 2 && node.children && node.children.length > 0){
          //装配
          child.push(node);
        }
      }
      if (node.children && node.children.length > 0) {
        node.children.forEach(child => traverse(child))
      }
    }

    traverse(data);
    return child;
  }

  findIndexByValue(obj, value) {
    let index = -1;
    Object.keys(obj).forEach((key, idx) => {
      if (obj[key] === value) {
        index = idx;
      }
    });
    return index;
  }
  async getProjectInfo() {
    let projectionInfo =await this.request?.execFunction(PluginCADToolFunctionType.GETPROJECTINFO, {});
    let projectId = projectionInfo?.data?.projectInfo?.projectId;
    return projectId;
  }
}

const fastenerStatisticsCommand = new FastenerStatisticsCommand();
export {fastenerStatisticsCommand};
